package test.sunyu.tools.hbase;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sunyu.tools.common.TimeTools;
import sunyu.tools.hbase.HbaseTools;

import java.util.Date;
import java.util.concurrent.Callable;

public class MyCallable implements Callable<Object> {

    private static final Logger logger = LoggerFactory.getLogger(Thread.currentThread().getClass());

    private int threadNum;
    private int dataSize;
    private TimeTools timeTools;
    private HbaseTools hbaseTools;
    private String tableName;
    private String rowKeyPre;
    private String familyName;
    private String qualifier;

    public MyCallable(int threadNum, int dataSize, TimeTools timeTools, HbaseTools hbaseTools, String tableName, String rowKeyPre, String familyName, String qualifier) {
        this.threadNum = threadNum;
        this.dataSize = dataSize;
        this.timeTools = timeTools;
        this.hbaseTools = hbaseTools;
        this.tableName = tableName;
        this.rowKeyPre = rowKeyPre;
        this.familyName = familyName;
        this.qualifier = qualifier;
    }

    @Override
    public Object call() throws Exception {
        Date start = new Date();
        Date d = timeTools.addYear(new Date(), -threadNum);
        for (int i = 0; i < dataSize; i++) {
            d = timeTools.addSecond(d, -1);
            String locationTime = timeTools.toString(d, "yyyyMMddHHmmss");
            logger.debug(Thread.currentThread().getName() + ":put:" + i + ":" + rowKeyPre + locationTime);
            this.hbaseTools.put(tableName, rowKeyPre + locationTime, familyName, qualifier, locationTime);
        }
        return String.format(Thread.currentThread().getName() + "执行时间：%s", timeTools.getIntervalString(start, new Date()));
    }

}
